package com.mybatis.mapper;

import com.mybatis.domain.User;
import com.mybatis.plugin.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;

import java.util.List;
import java.util.Map;

@CacheNamespace
public interface UserMapper {

    @Select("select `id`, `login_name`, `name`, `password` from `user` where id=#{id}")
    User selectById(@Param("id") Long id);

    /**
     * 可以指定statementType，默认是PREPARED
     */
    @Select("select `id`, `login_name`, `name`, `password` from `user` where id=#{id}")
    @Options(statementType = StatementType.PREPARED)
    User selectById2(@Param("id") Long id);

    @Select("select `id`, `login_name`, `name`, `password` from `user` where id=#{id}")
    @Options(flushCache = Options.FlushCachePolicy.TRUE)
    User selectByIdNoCache(@Param("id") Long id);

    @Select("select `id`, `login_name`, `name`, `password` from `user` where id=#{arg0} and name=#{arg1}")
    User selectByIdAndName(Long id, String name);

    @Select("select `id`, `login_name`, `name`, `password` from `user` where id=#{id} and name=#{arg1}")
    User selectByIdAndName2(@Param("id") Long id, String name);

    @Select("select `id`, `login_name`, `name`, `password` from `user` where id=#{id} and name=#{param2.name}")
    User selectByIdAndUser(@Param("id") Long id, User user);

    @Select("select `id`, `login_name`, `name`, `password` from `user` where id=#{id} and name=#{user.name}")
    User selectByIdAndUser2(@Param("id") Long id, @Param("user") User user);

    /**
     * JDK 1.8之后支持基于反射转换为变量名
     */
    @Select("select `id`, `login_name`, `name`, `password` from `user` where id=#{arg0} and name=#{arg1.name}")
    User selectByIdAndUser3(Long id, User user);

    void editUser(User user);

    User findUser();

    List<User> findByIds(Map<String, Object> ids);

    @Select("select * from `user`")
    List<User> findAllUsers(Page page);

    List<User> selectByUser(@Param("user") User user, Page page);

}
